// Copyright (c) 2021, yuxinyong and contributors
// For license information, please see license.txt

frappe.ui.form.on('Multi Level MRP', {
	setup: function (frm) {
		frm.set_query('sales_order', {
			'company': frm.doc.company,
			'docstatus': 1,
			'status': ['in',("To Deliver","To Deliver and Bill")]});
	},
	refresh: function(frm) {
		frm.add_custom_button(__("Stock Requirements List"), ()=> {
			frappe.set_route('query-report', 'Stock Requirements List', {mrp: frm.doc.name});
		}, __('View'));

		if (frm.doc.docstatus === 0) {
			if (!frm.is_new() && frm.doc.docstatus == 0) {
				frm.add_custom_button(__("Start Plan"), ()=> {
					frappe.call({
						method: "zelin_am.agile_manufacturing.doctype.multi_level_mrp.multi_level_mrp.run_mrp_multi_level",
						args:{
							mrp_doc_name: frm.doc.name
						},
						freeze: true,
						freeze_message: __("Plan running..."),
						callback: function(r) {
							frm.reload_doc();
						}
					});
				});
			}
		}
	},
	get_plan_items: function(frm) {
		frappe.call({
			method: "get_plan_items",
			doc: frm.doc,
			freeze: true,
			freeze_message: __("running..."),
			callback: function(r) {
				refresh_field("items");
			}
		});
	},
});
